package com.easydl.project.amqp.configuration;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.core.TopicExchange;
//import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Converter
 * n.	使发生转化的人(或物); 转换器; 整流器; 变流器; (改变无线电信号的) 变频器;
 * @author fxb208
 *
 */
@Configuration
//@EnableRabbit
public class EasyAmqpConfiguration {
	
	@Bean
	public MessageConverter messageConverter() {
		return new Jackson2JsonMessageConverter();
	}
	
	@Bean
	public Queue firstQueue() {
		// 创建一个队列，名称为：enjoy
		return new Queue("easyMask");
	}
	
	@Bean
	public TopicExchange easydlDiscern() {
		// 创建一个队列，名称为：enjoy
		return new TopicExchange("easydlDiscern");
	}
	
	
	/**
	 * 死信队列
	 * https://blog.csdn.net/zhangcongyi420/article/details/100126666
	 * 
	 * https://blog.csdn.net/qq_42606051/article/details/83505233
	 */
	@Bean
	public Queue margoRabbitQueue() {
		Map<String, Object> arguments=new HashMap<String, Object>();
		arguments.put("x-dead-letter-exchange", "deadExchange");
		arguments.put("x-dead-letter-queue", "deadQueue");
		arguments.put("x-dead-letter-routing-key", "deadQueue");
		//arguments.put("x-message-ttl" , 15*1000);
		return QueueBuilder.durable("margoRabbitQueue").withArguments(arguments).build();
		
	
	}
	
	/**
	 * 正常交换器
	 * @return
	 */
	@Bean
	public TopicExchange margoRabbitExchange() {
		return new TopicExchange("margoRabbitExchange");
	}
	
	/**
	 * 绑定
	 * @return
	 */
	@Bean
	public Binding margoRabbitBinding() {
		return new Binding("margoRabbitQueue", Binding.DestinationType.QUEUE, "margoRabbitExchange", "margoRabbit", null);
	}
	
	
	/**
	 * 死信交换器
	 * @return
	 */
	@Bean
	public DirectExchange deadExchange() {
		return new DirectExchange("deadExchange");
	}

	/**
	 * 死信队列
	 * @return
	 */
	@Bean
	public Queue deadQueue() {
		return new Queue("deadQueue");
	}
	
	@Bean
	public Binding margoRabbitDeadBinding() {
		return new Binding("deadQueue", Binding.DestinationType.QUEUE, "deadExchange", "deadQueue", null);
	}
}
